package com.vip.common.util;

import java.time.*;
import java.util.Date;

/**
 * LocalDateTimeUtils
 *
 * @author wgb
 * @date 2020/5/26 19:56
 */
public class LocalTimeUtils {
    /**
     * LocalDate转Date
     *
     * @param localDate
     * @return
     */
    public static Date localDateToDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * LocalDateTime转Date
     *
     * @param localDateTime
     * @return
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * Date转LocalDate
     *
     * @param date
     * @return
     */
    public static LocalDate dateToLocalDate(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
    }

    /**
     * Date转LocalDateTime
     *
     * @param date
     * @return
     */
    public static LocalDateTime dateToLocalDateTime(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    /**
     * LocalDateTime转TimeStamp
     *
     * @param localDateTime
     * @return
     */
    public static Long localDateTimeToTimeStamp(LocalDateTime localDateTime) {
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    /**
     * TimeStamp转LocalDateTime
     *
     * @param timestamp
     * @return
     */
    public static LocalDateTime timeStampToLocalDateTime(Long timestamp) {
        return new Date(timestamp).toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
    }

}
